package com.hqyj.demo.controller;
import com.hqyj.demo.entity.Goodstype;
import com.hqyj.demo.entity.vo.GoodsVo;
import io.swagger.models.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.demo.entity.vo.LikeQueryVo;

import com.hqyj.demo.service.GoodsService;
import com.hqyj.demo.entity.Goods;

import com.hqyj.demo.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author smj
 * @since 2022-10-09
 */

@RestController
@RequestMapping("/goods")
public class GoodsController {
     @Autowired
     private GoodsService goodsService;

     //    保存方法
     @PostMapping("/save")
     public R save(@RequestBody Goods goods){
        boolean save = goodsService.save(goods);
        if(save){
           return R.ok().message("保存数据成功！");
        }else{
           return R.error().message("保存数据失败！");
        }
     }
     //    删除单条数据
     @DeleteMapping("/delOne/{id}")
     public R delOne(@PathVariable Integer id){
        boolean b = goodsService.removeById(id);
        if(b)
           return R.ok().message("数据删除成功");
        else
           return R.ok().message("数据删除失败");
     }
     //  批量删除
     @DeleteMapping("/delBatch")
     public R delBatch(@RequestBody List<Integer> ids){
        boolean b = goodsService.removeBatchByIds(ids);
        if(b)
           return R.ok().message("批量删除成功");
        else
           return R.ok().message("批量删除失败");
     }
    //    修改数据
     @PutMapping("/update")
     public R update(@RequestBody Goods goods){
        boolean save = goodsService.updateById(goods);
        if(save){
           return R.ok().message("数据修改成功！");
        }else{
          return R.error().message("数据修改失败！");
        }
     }

     //修改多条记录状态
     @PutMapping("/saveUpdate")
     public R saveUpdate(@RequestBody List<Goods> list) {
          //logger.info (list.toString ());
        if (list != null) {
             boolean result = goodsService.updateBatchById (list);
             if (result) {
                  return R.ok ().message ("保存成功！");
             }
             return R.error ().message ("保存失败!");
        }
        return R.error ().message ("没有参数!");
     }

     //    模糊查询方法
     @PostMapping("/likeQuery")
     public R likeParamsQuery(@RequestBody GoodsVo goodsVo){
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(goodsVo.getName ())){
          queryWrapper.like("name",goodsVo.getName());
        }
         System.out.println(goodsVo);

         if(goodsVo.getTypeId()!=1 && goodsVo.getTypeId() != 0){
             queryWrapper.eq("type_id",goodsVo.getTypeId());
         }


        Page<Goods> page = goodsService.page(new Page<>(goodsVo.getCurrentPage (), goodsVo.getSizePage ()), queryWrapper);
        long total = page.getTotal();//总记录数
        List<Goods> list = page.getRecords();
        return R.ok().message ("查询成功！").data ("list",list).data ("total",total);
     }
     //多表查询
    @PostMapping("/queryManyGoods")
    public R queryManyGoods(){
         List<Goods> goodsList = goodsService.queryManyGoods();
         return R.ok().message("获取库存菜单成功").data("goodsList",goodsList);

    }
}
